package com.rerun.controller.system;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Maps;
import com.rerun.common.anno.Log;
import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.entity.Dictionaries;
import com.rerun.entity.DictionariesCategory;
import com.rerun.service.IDictionariesCategoryService;
import com.rerun.service.IDictionariesService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据字典
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /9/30 9:41
 */
@Controller
@RequestMapping("/system/dictionary")
public class DictionaryController extends SuperController {
	@Resource
	private IDictionariesCategoryService iDictionariesCategoryService;
	@Resource
	IDictionariesService dictionariesService;

	/**
	 * List string.
	 *
	 * @param pageNumber
	 *            the page number
	 * @param pageSize
	 *            the page size
	 * @param search
	 *            the search
	 * @param model
	 *            the model
	 * @return the string
	 */
	@RequestMapping("/list/{pageNumber}")
	public String list(@PathVariable Integer pageNumber, @RequestParam(defaultValue = "15") Integer pageSize,
			String search, Model model) {
		Page<DictionariesCategory> page = getPage(pageNumber, pageSize);
		model.addAttribute("pageSize", pageSize);
		// 查询分页

		Page<DictionariesCategory> pageData = iDictionariesCategoryService.selectPage(page);
		model.addAttribute("pageData", pageData);
		return "system/dictionary/list";
	}

	/**
	 * 增加字典分类
	 *
	 * @param model
	 *            the model
	 * @return the string
	 */
	@RequestMapping("/toAdd")
	public String add(Model model) {
		// EntityWrapper<Dictionaries> wrapper = new EntityWrapper<>();
		// wrapper.orderBy("sort", true);
		// wrapper.eq("parentId", "0");
		// List<Dictionaries> dictionaries = dictionariesService.selectList(wrapper);
		EntityWrapper<DictionariesCategory> entityWrapper = new EntityWrapper<>();
		entityWrapper.orderBy("categoryId", true);
		List<DictionariesCategory> categories = iDictionariesCategoryService.selectList(entityWrapper);
		model.addAttribute("list", categories);
		return "system/dictionary/add";

	}

	@Log("创建字典分类")
	@RequestMapping("/doAddCatgory")
	@ResponseBody
	public Rest doAddCategary(DictionariesCategory category) {
		try {
			iDictionariesCategoryService.insertAllColumn(category);
			return Rest.ok("ok");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("error");
	}

	@RequestMapping("/toEdit/{id}")
	public String edit(Model model, @PathVariable String id) {
		EntityWrapper<DictionariesCategory> wrapper = new EntityWrapper<>();
		wrapper.eq("categoryId", id);
		DictionariesCategory category = iDictionariesCategoryService.selectOne(wrapper);
		model.addAttribute("obj", category);
		return "system/dictionary/edit";

	}

	@Log("编辑字典分类")
	@RequestMapping("/doEditCategory")
	@ResponseBody
	public Rest doEditCategary(DictionariesCategory category) {
		try {
			EntityWrapper<DictionariesCategory> wrapper = new EntityWrapper<>();
			wrapper.eq("categoryId", category.getCategoryId());
			iDictionariesCategoryService.update(category, wrapper);
			return Rest.ok("ok");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("error");
	}

	@Log("删除字典分类")
	@RequestMapping("/doDelCategory")
	@ResponseBody
	public Rest doDelCategary(DictionariesCategory category) {
		try {
			EntityWrapper<DictionariesCategory> wrapper = new EntityWrapper<>();
			wrapper.eq("categoryId", category.getCategoryId());
			iDictionariesCategoryService.delete(wrapper);
			return Rest.ok("ok");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("error");
	}

	/**
	 * 查看字典信息
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/view/{code}")
	public String view(Model model, @PathVariable String code) {
		List<List<Dictionaries>> lists = dictionariesService.selectDictionariesByCode(code);
		if (lists != null && lists.size() > 0) {
			for (List<Dictionaries> list : lists) {
				for (Dictionaries dictionaries : list) {
					if (0 != dictionaries.getParentId()) {
						dictionaries.setDvalue("----" + dictionaries.getDvalue());
					}
				}
			}
		}
		model.addAttribute("list", lists);
		return "system/dictionary/dicList";

	}

	@Log("添加字典")
	@RequestMapping(value = "/addDictionary")
	@ResponseBody
	public Rest doAddDictionaries(Dictionaries dictionaries) {
		try {
			dictionariesService.insert(dictionaries);
			return Rest.ok("ok");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("error");
	}

	@Log("添加字典")
	@RequestMapping(value = "/json")
	@ResponseBody
	public Rest json(Dictionaries dictionaries) {
		try {
			System.out.println(dictionaries.getCategoryId());
			EntityWrapper<Dictionaries> entityWrapper = new EntityWrapper<>();
			entityWrapper.eq("categoryId", dictionaries.getCategoryId());
			entityWrapper.eq("parentId", "0");
			List<Dictionaries> dictionaryList = dictionariesService.selectList(entityWrapper);
			List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
			for (Dictionaries dictionary : dictionaryList) {
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", dictionary.getId());
				map.put("text", dictionary.getDvalue());
				listMap.add(map);
			}
			return Rest.okData(listMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("error");
	}
	
	/**
	 * 跳转编辑界面
	 * @param dictionaries
	 * @param model
	 * @return
	 */
	@RequestMapping("/toEditDic")
	public String toEditDic(Dictionaries dictionaries, Model model) {
		Dictionaries dictionary = dictionariesService.selectById(dictionaries.getId());
		if (dictionary != null) {
			Long parentId = dictionary.getParentId();
			if (parentId != 0) {
				EntityWrapper<Dictionaries> entityWrapper = new EntityWrapper<>();
				entityWrapper.eq("parentId", "0");
				entityWrapper.eq("categoryId", dictionary.getCategoryId());
				List<Dictionaries> list = dictionariesService.selectList(entityWrapper);
				model.addAttribute("listDics", list);
			}

			EntityWrapper<DictionariesCategory> entityWrapper = new EntityWrapper<>();
			entityWrapper.orderBy("categoryId", true);
			List<DictionariesCategory> categories = iDictionariesCategoryService.selectList(entityWrapper);
			model.addAttribute("list", categories);
		}
		model.addAttribute("obj", dictionary);
		return "/system/dictionary/editDic";

	}
	
	@Log("编辑字典")
	@RequestMapping("/doEditDic")
	@ResponseBody
	public Rest doeEditDic(Dictionaries dictionaries) {
		EntityWrapper<Dictionaries> entityWrapper = new EntityWrapper<>();
		entityWrapper.eq("id", dictionaries.getId());
		try {
			dictionariesService.update(dictionaries, entityWrapper);
			return Rest.ok("ok");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("error");
	}
	
	@Log("删除字典")
	@RequestMapping("/doDelDic")
	@ResponseBody
	public Rest doDelDic(Dictionaries dictionaries) {
		EntityWrapper<Dictionaries> entityWrapper = new EntityWrapper<>();
		entityWrapper.eq("id", dictionaries.getId());
		try {
			dictionariesService.delete(entityWrapper);
			return Rest.ok("ok");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("error");
	}
}
